//
//  KZSortView.swift
//  BoyeStore
//
//  Created by 朱凯 on 2018/6/27.
//  Copyright © 2018年 itboye. All rights reserved.
//

import UIKit

class KZSortView: UIView {
    
    
    // Int：0，默认；1：销量；2：价格
    // Bool：是否从低到高
    var sortClouse: (Int, Bool?) -> Void = { _, _ in }
    
    var preSelectedButton: UIButton! = nil

    convenience init() {
        
        self.init(frame: .zero)
        
        setupUI()
    }

}

// MARK: ------ 布局
extension KZSortView {
    func setupUI() {
        
        let titles = ["默认", "销量", "价格"]
        var last: UIView? = nil
        for (i, title) in titles.enumerated() {
            
            let button = UIButton(type: .custom)
            button.tag = i
            button.addTarget(self, action: #selector(onButtonClick(button:)), for: .touchUpInside)
            button.setTitle(title, for: .normal)
            button.titleLabel?.font = kFont(14)
            button.setTitleColor(kThemeColor, for: .selected)
            button.setTitleColor(UIColor.black, for: .normal)
            self.addSubview(button)
            
            if i == 0 {
                button.isSelected = true
                preSelectedButton = button
            }
            
            button.snp.makeConstraints { (m) in
                m.top.height.equalTo(self)
                if let last = last {
                    m.left.equalTo(last.snp.right)
                } else {
                    m.left.equalTo(0)
                }
                
                m.width.equalTo(self).multipliedBy(1.0/3)
            }
            
            if title == "价格" {
                let upButton = UIButton(type: .custom)
                upButton.tag = 10
                upButton.setImage(#imageLiteral(resourceName: "搜索-排序上"), for: .normal)
                upButton.setImage(#imageLiteral(resourceName: "搜索-排序上-sel"), for: .selected)
                button.addSubview(upButton)
                
                let downButton = UIButton(type: .custom)
                downButton.tag = 20
                downButton.setImage(#imageLiteral(resourceName: "搜索-排序下"), for: .normal)
                downButton.setImage(#imageLiteral(resourceName: "搜索-排序下-sel"), for: .selected)
                button.addSubview(downButton)
                
                upButton.snp.makeConstraints { (m) in
                    m.left.equalTo(button.snp.centerX).offset(20)
                    m.centerY.equalTo(button.snp.centerY).offset(-3)
                }
                downButton.snp.makeConstraints { (m) in
                    m.left.equalTo(upButton.snp.left)
                    m.centerY.equalTo(button.snp.centerY).offset(3)
                }
            }
            
            last = button
        }
    }
}

// MARK: ------ 事件
extension KZSortView {
    
    @objc func onButtonClick(button: UIButton) {
        
        if button == preSelectedButton {
            if button.tag == 2 {
                let upButton = button.viewWithTag(10) as! UIButton
                let downButton = button.viewWithTag(20) as! UIButton
                upButton.isSelected = !upButton.isSelected
                downButton.isSelected = !downButton.isSelected
                
                sortClouse(2, upButton.isSelected)
                
            } else {
                return
            }
        }
        else {
            for subview in self.subviews {
                if let button = subview as? UIButton {
                    button.isSelected = false
                    if button.tag == 2 {
                        let upButton = button.viewWithTag(10) as! UIButton
                        let downButton = button.viewWithTag(20) as! UIButton
                        upButton.isSelected = false
                        downButton.isSelected = false
                    }
                }
            }
            
            button.isSelected = true
            preSelectedButton = button
            
            if button.tag == 2 {
                let upButton = button.viewWithTag(10) as! UIButton
                upButton.isSelected = true
                
                sortClouse(2, true)
            } else {
                sortClouse(button.tag, nil)
            }
        }
        
        
    }
}
